﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V5_Model;
using V5_Common;
using System.Data;
using V5_DAL.SqlServer;

namespace V5_BLL {
    public class BLLUser {
        DALUser dal = new DALUser();
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId() {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID) {
            return dal.Exists(ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelUser model) {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelUser model) {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID) {

            dal.Delete(ID);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelUser GetModel(int ID) {

            return dal.GetModel(ID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中。
        /// </summary>
        public ModelUser GetModelByCache(int ID) {

            string CacheKey = "ModelUserModel-" + ID;
            object objModel = DataCacheHelper.GetCache(CacheKey);
            if (objModel == null) {
                try {
                    objModel = dal.GetModel(ID);
                    if (objModel != null) {
                        int ModelCache = ConfigHelper.GetConfigInt("ModelCache");
                        DataCacheHelper.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (ModelUser)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere) {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder) {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ModelUser> GetModelList(string strWhere) {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ModelUser> DataTableToList(DataTable dt) {
            List<ModelUser> modelList = new List<ModelUser>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0) {
                ModelUser model;
                for (int n = 0; n < rowsCount; n++) {
                    model = new ModelUser();
                    if (dt.Rows[n]["ID"].ToString() != "") {
                        model.ID = int.Parse(dt.Rows[n]["ID"].ToString());
                    }
                    model.UserName = dt.Rows[n]["UserName"].ToString();
                    model.UserPassword = dt.Rows[n]["UserPassword"].ToString();
                    if (dt.Rows[n]["RegDate"].ToString() != "") {
                        model.RegDate = DateTime.Parse(dt.Rows[n]["RegDate"].ToString());
                    }
                    if (dt.Rows[n]["UserScore"].ToString() != "") {
                        model.UserScore = int.Parse(dt.Rows[n]["UserScore"].ToString());
                    }
                    if (dt.Rows[n]["ScoreUsed"].ToString() != "") {
                        model.ScoreUsed = int.Parse(dt.Rows[n]["ScoreUsed"].ToString());
                    }
                    model.UserEmail = dt.Rows[n]["UserEmail"].ToString();
                    if (dt.Rows[n]["UserSex"].ToString() != "") {
                        model.UserSex = int.Parse(dt.Rows[n]["UserSex"].ToString());
                    }
                    if (dt.Rows[n]["LoginDate"].ToString() != "") {
                        model.LoginDate = DateTime.Parse(dt.Rows[n]["LoginDate"].ToString());
                    }
                    if (dt.Rows[n]["LoginTimes"].ToString() != "") {
                        model.LoginTimes = int.Parse(dt.Rows[n]["LoginTimes"].ToString());
                    }
                    model.IdentityCard = dt.Rows[n]["IdentityCard"].ToString();
                    if (dt.Rows[n]["PostCode"].ToString() != "") {
                        model.PostCode = int.Parse(dt.Rows[n]["PostCode"].ToString());
                    }
                    if (dt.Rows[n]["AreaID"].ToString() != "") {
                        model.AreaID = int.Parse(dt.Rows[n]["AreaID"].ToString());
                    }
                    model.Address3 = dt.Rows[n]["Address3"].ToString();
                    model.Address2 = dt.Rows[n]["Address2"].ToString();
                    model.Address = dt.Rows[n]["Address"].ToString();
                    if (dt.Rows[n]["IsLock"].ToString() != "") {
                        model.IsLock = int.Parse(dt.Rows[n]["IsLock"].ToString());
                    }
                    if (dt.Rows[n]["GroupID"].ToString() != "") {
                        model.GroupID = int.Parse(dt.Rows[n]["GroupID"].ToString());
                    }
                    model.Question = dt.Rows[n]["Question"].ToString();
                    model.Answer = dt.Rows[n]["Answer"].ToString();
                    model.LoginIP = dt.Rows[n]["LoginIP"].ToString();
                    if (dt.Rows[n]["FrozenDeposit"].ToString() != "") {
                        model.FrozenDeposit = decimal.Parse(dt.Rows[n]["FrozenDeposit"].ToString());
                    }
                    if (dt.Rows[n]["FrozenScore"].ToString() != "") {
                        model.FrozenScore = int.Parse(dt.Rows[n]["FrozenScore"].ToString());
                    }
                    model.BirthDay = dt.Rows[n]["BirthDay"].ToString();
                    model.QQ = dt.Rows[n]["QQ"].ToString();
                    model.Telephone = dt.Rows[n]["Telephone"].ToString();
                    model.Phone = dt.Rows[n]["Phone"].ToString();
                    model.TrueName = dt.Rows[n]["TrueName"].ToString();
                    model.CommendUser = dt.Rows[n]["CommendUser"].ToString();
                    model.NewPassWord = dt.Rows[n]["NewPassWord"].ToString();
                    model.GUID = dt.Rows[n]["GUID"].ToString();
                    model.Readme = dt.Rows[n]["Readme"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList() {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            return dal.GetList(PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
        }

        #endregion  成员方法

        public List<ModelUser> GetModelList(int top, string strWhere, string fileOrder) {
            DataSet ds = dal.GetList(top, strWhere, fileOrder);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 检查注册用户
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool CheckRegUser(string username) {
            DataTable dt = this.GetList(1,
                string.Format(" UserName='{0}' ", username),
                " ID ").Tables[0];
            if (dt != null && dt.Rows.Count > 0) {
                return false;
            }
            return true;
        }

        //用户登录
        public ModelUser UserLogin(string username, string userpwd) {
            DataTable dt = this.GetList(string.Format("UserName='{0}' and UserPassword='{1}' ", username,
                userpwd)).Tables[0];
            if (dt != null && dt.Rows.Count > 0) {
                return this.GetModel(InputHelper.SetNumber(dt.Rows[0]["ID"]));
            }
            return null;
        }

        /// <summary>
        /// 得到用户组名称
        /// </summary>
        /// <param name="groupid"></param>
        /// <returns></returns>
        public string GetUserGroup(int groupid) {
            if (groupid > 0) {
                return new BLLUserGroup().GetModel(groupid).GroupName;
            }
            return "普通会员";
        }
    }
}
